package org.agilelang.editor.dialog;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import org.agilelang.editor.syntaxtext.dialogpopup.DialogBase;

public class DlgGenerate extends DialogBase {

    final JComboBox combobox = new JComboBox();
    JTextField path = new JTextField(25);

    public DlgGenerate() {
        super("Generator", 2);
        initComponents();
        initUI();
    }

    private void initComponents() {


        final JSeparator sep = new JSeparator();
        final ListCellRenderer lcr = combobox.getRenderer();
        combobox.setRenderer(new ListCellRenderer() {

            public Component getListCellRendererComponent(
                    JList list, Object value, int index,
                    boolean isSelected, boolean cellHasFocus) {
                MyItem item = (MyItem) value;
                JLabel label = (JLabel) lcr.getListCellRendererComponent(
                        list, item, index, isSelected, cellHasFocus);
                if (item.hasSeparator()) {
                    label.setBorder(
                            BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY));
                } else {
                    label.setBorder(BorderFactory.createEmptyBorder());
                }
                return label;
            }
        });
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        /*model.addElement(new MyItem("Java Swing"));
        model.addElement(new MyItem("wxPython"));
        model.addElement(new MyItem("Java Struts2", true));
        model.addElement(new MyItem("GAE Python"));*/
        model.addElement(new MyItem("CakePHP"));

        combobox.setModel(model);
        combobox.setEditable(false);

        path.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {
                JFileChooser fileChooser = new JFileChooser();

                fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int result = fileChooser.showSaveDialog(null);
                if (result == JFileChooser.CANCEL_OPTION) {
                    return;
                }
                path.setText(fileChooser.getSelectedFile().getAbsolutePath());
            }
        });

        this.addLineTxt("Generator", combobox);
        this.addLineTxt("Path", path);

    }

    public String getGenerator() {
        MyItem item = (MyItem) combobox.getSelectedItem();
        return item.item;
    }

    public String getPath() {
        return path.getText();
    }
    
    public void setPath(String path) {
        this.path.setText(path);
    }

    class MyItem {

        private final String item;
        private final boolean flag;

        public MyItem(String str) {
            this(str, false);
        }

        public MyItem(String str, boolean flg) {
            item = str;
            flag = flg;
        }

        public String toString() {
            return item;
        }

        public boolean hasSeparator() {
            return flag;
        }
    }

    
}